home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998…eptember: Technology Seed / September 98 ADC Seed CD.toast / FireWire 1.1 DR2 SDK / Source / OpenTransport / Interfaces / OpenTptCommon.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-15  |  12.9 KB  |  486 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OpenTptCommon.h
  3.  
  4.     Contains:    Equates for Open Transport development needed both by clients
  5.                 and by kernel.
  6.  
  7.     Copyright:    © 1993-1996 by Apple Computer, Inc. and Mentat Inc., all rights reserved.
  8.  
  9.     Change History (most recent first):
  10.  
  11.          <6>     2/13/96    pfl        • Fix up the align options.
  12.          <5>    12/19/95    pfl        Use PRAGMA_ALIGN_SUPPORTED
  13.          <4>    10/23/95    mjq        • Fix up problems with MI_LITTLE_ENDIAN/MI_BIG_ENDIAN
  14.                                     • Change a -1L to 0xffffffffU                                    
  15.          <3>    10/07/95    mjq        • Fix up a bug in the HiPriProtocolEvent stuff.
  16.          <2>     9/05/95    mjq        Include definition of I_OTYieldPort
  17.          <1>     9/05/95    mjq        • Move OTBuffer stuff into OpenTransport.h
  18.                                     • Move OTBand back into OpenTransport.h
  19.                                     • Renamed from OpenTptDev.h
  20.  
  21.     To Do:
  22.  
  23. */
  24.  
  25. #ifndef __OPENTPTCOMMON__
  26. #define __OPENTPTCOMMON__
  27.  
  28. #ifndef __OPENTRANSPORT__
  29. #include <OpenTransport.h>
  30. #endif
  31. #ifndef _MPS_STREAM_
  32. #include <mistream.h>
  33. #endif
  34.  
  35. #if GENERATING68K && defined(__MWERKS__)
  36. #pragma pointers_in_D0
  37. #endif
  38.  
  39. #if PRAGMA_ALIGN_SUPPORTED
  40.     #if GENERATING68K
  41.     #pragma options align=mac68k
  42.     #endif
  43.     #if GENERATINGPOWERPC
  44.     #pragma options align=power
  45.     #endif
  46. #endif
  47.  
  48. /*******************************************************************************
  49. ** Some typedefs
  50. ********************************************************************************/
  51.  
  52. typedef long        OTCommand;        /* The command code in STREAMS messages        */
  53. typedef SInt16        OTRelease;        /* An internal OT typedef                    */
  54.  
  55. /*******************************************************************************
  56. ** Some private IOCTL constants we need to know
  57. ********************************************************************************/
  58.  
  59. enum
  60. {
  61.     I_OTYieldPort    = MIOC_CMD(MIOC_OT, 4)    /* request to yield the port                */
  62. };
  63.  
  64. /*******************************************************************************
  65. ** Some defines
  66. ********************************************************************************/
  67.  
  68. #define MI_BIG_ENDIAN            1
  69. #undef    MI_LITTLE_ENDIAN
  70.  
  71. //
  72. // For static members that need to match pascal functions
  73. //
  74. #define _FSDECL                static pascal
  75. #define _FSDEF                pascal
  76. #define _MDEF                /* %%% Temporary this line deleted for release disk */
  77.  
  78. /*    -------------------------------------------------------------------------
  79.     ** Bitmap functions
  80.     **
  81.     ** These functions deal with a bitmap that is multiple-bytes long
  82.     ------------------------------------------------------------------------- */
  83.  
  84. #ifdef __cplusplus
  85. extern "C" {
  86. #endif
  87.     //
  88.     // Set the first clear bit in "bitMap", starting with bit "startBit",
  89.     // giving up after "numBits".  Returns the bit # that was set, or
  90.     // a kOTNotFoundErr if there was no clear bit available
  91.     //
  92. OTResult    OTSetFirstClearBit(UInt8* bitMap, size_t startBit, size_t numBits);
  93.     //
  94.     // Standard clear, set and test bit functions
  95.     //
  96. Boolean        OTClearBit(UInt8* bitMap, size_t bitNo);
  97. Boolean        OTSetBit(UInt8* bitMap, size_t bitNo);
  98. Boolean        OTTestBit(UInt8* bitMap, size_t bitNo);
  99.  
  100. #ifdef __cplusplus
  101. }
  102. #endif
  103.  
  104. /*    -------------------------------------------------------------------------
  105.     ** OTHashList
  106.     **
  107.     ** This implements a simple, but efficient hash list.  It is not
  108.     ** thread-safe.
  109.     ------------------------------------------------------------------------- */
  110.  
  111. typedef struct OTHashList    OTHashList;
  112.  
  113. typedef UInt32 (* _CDECL OTHashProcPtr)(OTLink* linkToHash);
  114. typedef Boolean (* _CDECL OTHashSearchProcPtr)(const void* ref, OTLink* linkToCheck);
  115.  
  116. #ifdef __cplusplus
  117. extern "C" {
  118. #endif
  119.     //
  120.     // Return the number of bytes of memory needed to create a hash list
  121.     // of at least "numEntries" entries.
  122.     //
  123. size_t        OTCalculateHashListMemoryNeeds(size_t numEntries);
  124.     //
  125.     // Create an OTHashList from "memory".  Return an error if it
  126.     // couldn't be done.
  127.     //
  128. OTResult    OTInitHashList(void* memory, size_t numBytes, OTHashProcPtr);
  129. void        OTAddToHashList(OTHashList*, OTLink*);
  130. Boolean        OTRemoveLinkFromHashList(OTHashList*, OTLink*);
  131. Boolean        OTIsInHashList(OTHashList*, OTLink*);
  132. OTLink*        OTFindInHashList(OTHashList*, OTHashSearchProcPtr proc,
  133.                             const void* refPtr, UInt32 hashValue);
  134. OTLink*        OTRemoveFromHashList(OTHashList*, OTHashSearchProcPtr proc,
  135.                                  const void* refPtr, UInt32 hashValue);
  136.  
  137. #ifdef __cplusplus
  138. }
  139. #endif
  140.  
  141. struct OTHashList
  142. {
  143.     OTHashProcPtr        fHashProc;
  144.     size_t                fHashTableSize;
  145.     OTLink**            fHashBuckets;
  146.  
  147. #ifdef __cplusplus
  148.             void        Add(OTLink* toAdd)
  149.                             { OTAddToHashList(this, toAdd); }
  150.                             
  151.             Boolean        RemoveLink(OTLink* toRemove)
  152.                             { return OTRemoveLinkFromHashList(this, toRemove); }
  153.                             
  154.             OTLink*        Remove(OTHashSearchProcPtr proc,
  155.                                const void* refPtr, UInt32 hashValue)
  156.                             { return OTRemoveFromHashList(this, proc, refPtr, hashValue); }
  157.                             
  158.             Boolean        IsInList(OTLink* toFind)
  159.                             { return OTIsInHashList(this, toFind); }
  160.                             
  161.             OTLink*        FindLink(OTHashSearchProcPtr proc, const void* refPtr,
  162.                                  UInt32 hash)
  163.                             {    return OTFindInHashList(this, proc, refPtr, hash); }
  164. #endif
  165. };
  166.  
  167.  
  168. /*    -------------------------------------------------------------------------
  169.     ** Random functions
  170.     ** 
  171.     ** These implement a very simple random number generator
  172.     ------------------------------------------------------------------------- */
  173.  
  174.     #ifdef __cplusplus
  175.     extern "C" {
  176.     #endif
  177.     
  178.     UInt32    OTGetRandomSeed();
  179.     UInt32    OTGetRandomNumber(UInt32* seed, UInt32 lo, UInt32 hi);
  180.     
  181.     #ifdef __cplusplus
  182.     }
  183.     #endif
  184.  
  185.  
  186. /*******************************************************************************
  187. ** XmitRecord Class
  188. ** 
  189. ** This class is used when sending "write"-type commands, where we need
  190. ** information to be able to complete a write to the client when it is 
  191. ** finished.
  192. ********************************************************************************/
  193.  
  194. struct XmitRecord
  195. {
  196.         OTLink        fLink;
  197.         void*        provider;
  198.         void*        dataBuf;
  199.         size_t        dataLen;
  200.         SInt16        ackCount;
  201. };
  202.     
  203. /*******************************************************************************
  204. ** Miscellaneous functions
  205. ********************************************************************************/
  206.  
  207. #ifdef __cplusplus
  208. extern "C" {
  209. #endif
  210. //
  211. // Return true if we are at interrupt level
  212. //
  213. Boolean        OTIsAtInterruptLevel(void);
  214. //
  215. // Return true if we are currently at either system task time, or we
  216. // are running a deferred task procedure from system task time.
  217. //
  218. Boolean     OTCanLoadLibraries(void);
  219. void        OTEnterCriticalSection(void);
  220. void        OTLeaveCriticalSection(void);
  221.  
  222. #ifdef __cplusplus
  223. }
  224. #endif
  225.  
  226. /*******************************************************************************
  227. ** Port ref functions
  228. ********************************************************************************/
  229.  
  230. #ifdef __cplusplus
  231. extern "C" {
  232. #endif
  233.     
  234.     pascal OTPortRef    OTSetDeviceTypeInPortRef(OTPortRef ref, UInt16 devType);
  235.     pascal OTPortRef    OTSetBusTypeInPortRef(OTPortRef ref, UInt8 busType);
  236.  
  237.     pascal Boolean        OTMakePortName(char* buffer, const char* moduleName,
  238.                                        OTPortRef);
  239.     
  240. #ifdef __cplusplus
  241. }
  242. #endif
  243.     
  244. /*******************************************************************************
  245. ** Some defines
  246. ********************************************************************************/
  247.  
  248. /*    -------------------------------------------------------------------------
  249.     IOCtls used by OpenTransport
  250.     ------------------------------------------------------------------------- */
  251.  
  252.     typedef int    OTNotifyType;
  253.     
  254.     enum
  255.     {
  256.         kOTNotifyAllModules = 0, kOTNotifyInterestedModules = 1,
  257.         kOTNotifyControlModules = 2
  258.     };
  259.     
  260.     struct OTIOCtlNotifyInfo
  261.     {
  262.         OTEventCode    fCode;        // Event Code
  263.         void*        fCookie;    // Cookie associated with it
  264.         UInt32        fNotifyType;// Who to notify
  265.     };
  266.     
  267.     typedef struct OTIoctlNotifyInfo    OTIoctlNotifyInfo;
  268.  
  269.     enum
  270.     {
  271.         I_OTNotifyAllClients    = MIOC_CMD(MIOC_OT, 50),
  272.         I_OTSetPowerLevel        = MIOC_CMD(MIOC_OT, 51)
  273.     };
  274.     
  275. /*    -------------------------------------------------------------------------
  276.     Some equates for kPROTOCOLEVENTs that normal clients don't need to 
  277.     know.
  278.     ------------------------------------------------------------------------- */
  279.  
  280.     enum
  281.     {
  282.         kHiPriProtocolEvent        = 0x08000000    /* Bit to be a high-priority event        */
  283.     };
  284.  
  285.     #define IsHiPriProtocolEvent(x)        (((x) & 0xff000000) == (kPROTOCOLEVENT | kHiPriProtocolEvent))
  286.     #define StripProtocolEvent(x)        ((x) & ~kHiPriProtocolEvent)
  287.     //
  288.     // Use this template to define your preferences if you use
  289.     // the TStreamGroup and TStreamFamily infrastructure
  290.     //
  291.     #define OTPreferencesFields(num)            \
  292.         UInt16            fVersion;                \
  293.         UInt16            fNumPrefs;                \
  294.         OTPortRef        fPort;                    \
  295.         OTLink            fLink;                    \
  296.         void*            fPrefs[num]
  297.     //
  298.     // This template is used to define the first set
  299.     // of fields in each individual preference array
  300.     //
  301.     #define OTPreferenceFields                    \
  302.         UInt16            fVersion;                \
  303.         UInt16            fSize
  304.         
  305.     //
  306.     // Define to tell infrastructure you want the main preference
  307.     // structure instead of a substructure.
  308.     //
  309.     enum
  310.     {
  311.         kOTPrefStructureCode = (UInt32)0xffffffffU
  312.     };
  313.     
  314.  
  315. /*******************************************************************************
  316. ** Functions to convert xti/mac errors to OSStatus'
  317. ********************************************************************************/
  318.  
  319. #ifdef __cplusplus
  320. extern "C" {
  321. #endif
  322.  
  323. pascal OSStatus OTConvertError(OTXTIErr xtiErr, OTUnixErr macErr);
  324.                     
  325. #ifdef __cplusplus
  326. }
  327. #endif
  328.  
  329. #ifdef __cplusplus
  330. //
  331. // Macro for Speed
  332. //
  333. inline OSStatus OTDoConvertError(long xtiErr, long macErr)
  334. {
  335.     return xtiErr == 0 ? kOTNoError : OTConvertError((OTXTIErr)xtiErr, (OTUnixErr)macErr);
  336. }
  337.  
  338. #else
  339.  
  340. #define OTDoConvertError(xtiErr, macErr)    \
  341.     ((xtiErr) == 0 ? kOTNoError : OTConvertError((OTXTIErr)(xtiErr), (OTUnixErr)(macErr)))
  342.     
  343. #endif
  344.  
  345. /*******************************************************************************
  346. ** Some efficient implementations of BigEndian and LittleEndian words
  347. ********************************************************************************/
  348.  
  349. #ifdef MI_BIG_ENDIAN
  350. //
  351. // Since Open Transport is 68020 and better only, we don't have to
  352. // worry about alignment problems.
  353. //
  354. #define GetBigEndian16At(ptr)        (*(UInt16*)(ptr))
  355. #define GetBigEndian32At(ptr)        (*(UInt32*)(ptr))    
  356. #define SetBigEndian16At(ptr, val)    { *(UInt16*)(ptr) = (UInt16)(val); }
  357. #define SetBigEndian32At(ptr, val)    { *(UInt32*)(ptr) = (UInt32)(val); }
  358.  
  359. #else
  360.  
  361. #define GetBigEndian16At(ptr)                        \
  362.         ((Uint16)(((((UInt8*)ptr)[0]) << 8) + ((UInt8*)ptr)[1]))
  363.  
  364. #define GetBigEndian32At(ptr)                        \
  365.         ((UInt32)(((((UInt8*)ptr)[0]) << 24) +        \
  366.                     (((UInt8*)ptr)[1]  << 16) +        \
  367.                     (((UInt8*)ptr)[2]  << 8) +        \
  368.                      ((UInt8*)ptr)[3]))
  369.  
  370. #define SetBigEndian16At(ptr, val)                \
  371.         {                                            \
  372.             ((UInt8*)ptr)[0] = (UInt8)(val >> 8);    \
  373.             ((UInt8*)ptr)[1] = (UInt8)(val);        \
  374.         }
  375.         
  376. #define SetBigEndian32At(ptr, val)                \
  377.         {                                            \
  378.             ((UInt8*)ptr)[0] = (UInt8)(val >> 24);    \
  379.             ((UInt8*)ptr)[1] = (UInt8)(val >> 16);    \
  380.             ((UInt8*)ptr)[2] = (UInt8)(val >> 8);    \
  381.             ((UInt8*)ptr)[3] = (UInt8)(val);        \
  382.         }
  383.  
  384. #endif
  385.         
  386. #ifdef MI_LITTLE_ENDIAN
  387.  
  388. #define GetLittleEndian16At(ptr)            (*(UInt16*)(ptr))
  389. #define GetLittleEndian32At(ptr)            (*(UInt32*)(ptr))    
  390. #define SetLittleEndian16At(ptr, val)    { *(UInt16*)(ptr) = (UInt16)(val); }
  391. #define SetLittleEndian32At(ptr, val)    { *(UInt32*)(ptr) = (UInt32)(val); }
  392.  
  393. #else
  394.  
  395. #define GetLittleEndian16At(ptr)                    \
  396.         ((Uint16)(((((UInt8*)ptr)[1]) << 8) + ((UInt8*)ptr)[0]))
  397.  
  398. #define GetLittleEndian32At(ptr)                    \
  399.         ((UInt32)(((((UInt8*)ptr)[3]) << 24) +        \
  400.                     (((UInt8*)ptr)[2]  << 16) +        \
  401.                     (((UInt8*)ptr)[1]  << 8) +        \
  402.                      ((UInt8*)ptr)[0]))
  403.  
  404. #define SetLittleEndian16At(ptr, val)                \
  405.         {                                            \
  406.             ((UInt8*)ptr)[1] = (UInt8)(val >> 8);    \
  407.             ((UInt8*)ptr)[0] = (UInt8)(val);        \
  408.         }
  409.         
  410. #define SetLittleEndian32At(ptr, val)                \
  411.         {                                            \
  412.             ((UInt8*)ptr)[3] = (UInt8)(val >> 24);    \
  413.             ((UInt8*)ptr)[2] = (UInt8)(val >> 16);    \
  414.             ((UInt8*)ptr)[1] = (UInt8)(val >> 8);    \
  415.             ((UInt8*)ptr)[0] = (UInt8)(val);        \
  416.         }
  417.  
  418. #endif
  419.  
  420. #ifdef __cplusplus
  421.  
  422. /*    -------------------------------------------------------------------------
  423.     TOTBaseObject
  424.     
  425.     This is the base class for Open Transport objects that DO NOT need to 
  426.     be 68K aligned.
  427.     ------------------------------------------------------------------------- */
  428.  
  429. #if USESINGLEOBJECT
  430.     class TOTBaseObject : public SingleObject
  431. #else
  432.     class TOTBaseObject
  433. #endif
  434.     {
  435.         EXTRA_VTABLE_SLOT
  436.         
  437.         public:
  438.                             _CT TOTBaseObject() {}
  439.             virtual            _DT TOTBaseObject();
  440.             
  441.         private:
  442.                             _CT TOTBaseObject(const TOTBaseObject&);
  443.                     void    operator=(const TOTBaseObject&);
  444.     };
  445.     
  446. #if PRAGMA_ALIGN_SUPPORTED
  447. #pragma options align=reset
  448. #pragma options align=mac68k
  449. #endif
  450.  
  451. /*    -------------------------------------------------------------------------
  452.     TOTAlignedObject
  453.  
  454.     This is the base class for Open Transport objects that DO need to 
  455.     be 68K aligned.
  456.     ------------------------------------------------------------------------- */
  457.  
  458. #if USESINGLEOBJECT
  459.     class TOTAlignedObject : public SingleObject
  460. #else
  461.     class TOTAlignedObject
  462. #endif
  463.     {
  464.         EXTRA_VTABLE_SLOT
  465.         
  466.         public:
  467.                             _CT TOTAlignedObject() {}
  468.             virtual            _DT TOTAlignedObject();
  469.  
  470.         private:
  471.                             _CT TOTAlignedObject(const TOTAlignedObject&);
  472.                     void    operator=(const TOTAlignedObject&);
  473.     };
  474.  
  475. #endif
  476.  
  477. #if GENERATING68K && defined(__MWERKS__)
  478. #pragma pointers_in_A0
  479. #endif
  480.  
  481. #if PRAGMA_ALIGN_SUPPORTED
  482. #pragma options align=reset
  483. #endif
  484.  
  485. #endif    /* __OPENTPTCOMMON__    */
  486.